package cn.iocoder.yudao.module.member.controller.admin.faceanalyserequest;

import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.faceanalyserequest.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.faceanalyserequest.FaceanalyseRequestDO;
import cn.iocoder.yudao.module.member.service.faceanalyserequest.FaceanalyseRequestService;

@Tag(name = "管理后台 - 会员皮肤状态分析请求")
@RestController
@RequestMapping("/member/faceanalyse-request")
@Validated
public class FaceanalyseRequestController {

    @Resource
    private FaceanalyseRequestService faceanalyseRequestService;

    @PostMapping("/create")
    @Operation(summary = "创建会员皮肤状态分析请求")
    @PermitAll
    @TenantIgnore
//    @PreAuthorize("@ss.hasPermission('member:faceanalyse-request:create')")
    public CommonResult<Long> createFaceanalyseRequest(@Valid @RequestBody FaceanalyseRequestSaveReqVO createReqVO) throws JsonProcessingException {
        return success(faceanalyseRequestService.createFaceanalyseRequest(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员皮肤状态分析请求")
    @PreAuthorize("@ss.hasPermission('member:faceanalyse-request:update')")
    public CommonResult<Boolean> updateFaceanalyseRequest(@Valid @RequestBody FaceanalyseRequestSaveReqVO updateReqVO) {
        faceanalyseRequestService.updateFaceanalyseRequest(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员皮肤状态分析请求")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:faceanalyse-request:delete')")
    public CommonResult<Boolean> deleteFaceanalyseRequest(@RequestParam("id") Long id) {
        faceanalyseRequestService.deleteFaceanalyseRequest(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员皮肤状态分析请求")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:faceanalyse-request:query')")
    public CommonResult<FaceanalyseRequestRespVO> getFaceanalyseRequest(@RequestParam("id") Long id) {
        FaceanalyseRequestDO faceanalyseRequest = faceanalyseRequestService.getFaceanalyseRequest(id);
        return success(BeanUtils.toBean(faceanalyseRequest, FaceanalyseRequestRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员皮肤状态分析请求分页")
    @PreAuthorize("@ss.hasPermission('member:faceanalyse-request:query')")
    public CommonResult<PageResult<FaceanalyseRequestRespVO>> getFaceanalyseRequestPage(@Valid FaceanalyseRequestPageReqVO pageReqVO) {
        PageResult<FaceanalyseRequestDO> pageResult = faceanalyseRequestService.getFaceanalyseRequestPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FaceanalyseRequestRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员皮肤状态分析请求 Excel")
    @PreAuthorize("@ss.hasPermission('member:faceanalyse-request:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFaceanalyseRequestExcel(@Valid FaceanalyseRequestPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FaceanalyseRequestDO> list = faceanalyseRequestService.getFaceanalyseRequestPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员皮肤状态分析请求.xls", "数据", FaceanalyseRequestRespVO.class,
                        BeanUtils.toBean(list, FaceanalyseRequestRespVO.class));
    }

    public static void main(String[] args) throws JsonProcessingException {
        String json="{\"api_Key\":\"123456789\"}";
//        JSONObject jsonObject = JSONObject.parseObject(json);
        FaceanalyseRequestSaveReqVO vo=JSON.parseObject( json,FaceanalyseRequestSaveReqVO.class);

        System.out.println(vo.toString());
//        System.out.println(JSONUtil.toJsonStr( vo));
//
//        //将对象名称驼峰转下划线
//        ObjectMapper mapper = new ObjectMapper();
//        // mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES); // 忽略Map中的null值
//        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 忽略所有为null的属性
//        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
//        String jsonr= mapper.writeValueAsString(vo);
//        System.out.println(jsonr);
//        //将json转为map
//        HashMap<String, Object> paramMap = new HashMap<>();
//        paramMap.putAll(mapper.readValue(jsonr, HashMap.class));
//        System.out.println(paramMap);
        //根据json


    }

}